var req;
var errorMsgId = "errorMessage";

var loginNameValid = false;
var passwordValid = false;
var valCodeValid = false;

var valid_classname = "valid";
var invalid_classname = "invalid";

var btnField = null;

$(document).ready(function(){
	$("#loginBtn").ajaxStart(function(){
		document.getElementById("loginBtn").disabled = "disabled";
    });
    $("#loginBtn").ajaxStop(function(){
    	document.getElementById("loginBtn").disabled = "";
    });
});

function keyDownHandle(event){
	if(event.keyCode == 13){
		var buttonField = document.getElementById("loginBtn");
		validateUserLogin(buttonField,'loginName','password','validateCode','autoLogin');
	}
}

/**
 * 
 */
function validateUserLogin(buttonField, loginNameId,passwordId,valCodeId,autoLoginId) {
	btnField = buttonField;
	buttonField.disabled = true;
	var loginNameField = document.getElementById(loginNameId);
	var passwordField = document.getElementById(passwordId);
	var valCodeField = document.getElementById(valCodeId);
	var msg = "";
	if(!notEmpty(loginNameField)){
		msg = "请输入登陆名";
	}else if(!notEmpty(passwordField)){
		msg = "请输入密码";
	}else if(!notEmpty(valCodeField)){
		msg = "请输入验证码";
	}
	
	if(msg != ""){
		setLoginMessage(errorMsgId,"<span></span>" + msg,valid_classname);
		buttonField.disabled = false;
		return;
	}
	
	var autoLoginField = document.getElementById(autoLoginId);
	var autoLoginFlag = autoLoginField.status ? "1" : "0";
	var loginName = loginNameField.value;
	var password = passwordField.value;
	var validateCode = valCodeField.value;
    var url = "loginActionPage.jsp?loginName="+ loginName + "&" + "password=" + 
    	password + "&validateCode=" + validateCode + "&autoLoginFlag=" + autoLoginFlag;
    url = encodeURI(encodeURI(url));
    $.get(url, null, callback);
}

function reloadcode(codeField){
	codeField.setAttribute('src','images/image.jsp?'+Math.random());
}

function callback(data) {
	setMsg($(data).text());
}

function setMsg(value) {
	if(!(value.indexOf("url:") == 0)){
		setLoginMessage(errorMsgId, "<span></span>" + value, invalid_classname);
		reloadInput();
	}else{
		var host = location.host;
		var url = value.substring(4);
		if(url.indexOf("http:") == 0){
			window.open(url, "_blank");
			return;
		}
		
		if(url.indexOf("/") != 0)
			url = "/" + url;
		var curUrl = host + url;
		location.reload("http://" + curUrl);
	}
}

/**
 * 验证输入值是否为空
 * @param item 输入框对象
 * @returns {Boolean}
 */
function notEmpty(item){
	var value = escape(item.value);
	if(value == "")
		return false;
	return true;
}

/**
 * 设置提示信息
 * @param itemId 对象的Id
 * @param msg 提示字数串
 * @param className 样式名称
 */
function setLoginMessage(itemId, msg, className){
	var mdiv = document.getElementById(itemId);
	mdiv.innerHTML = msg;
	mdiv.style.display = "block";
}

function reloadInput(){
	var passwordField = document.getElementById("password");
	var valCodeField = document.getElementById("validateCode");
	var imageCodeField = document.getElementById("imageCode");
	passwordField.value = "";
	valCodeField.value = "";
	reloadcode(imageCodeField);
	btnField.disabled = false;
}
